package com.ecommerce.recommendation.service.impl;

import com.ecommerce.recommendation.service.AnalysisService;
import com.ecommerce.recommendation.util.HdfsUtil;
import com.ecommerce.recommendation.util.MapReduceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class AnalysisServiceImpl implements AnalysisService {

    @Autowired
    private HdfsUtil hdfsUtil;

    @Autowired
    private MapReduceUtil mapReduceUtil;

    @Override
    public Map<String, Object> uploadFile(MultipartFile file) throws Exception {
        String hdfsPath = hdfsUtil.uploadFile(file);
        Map<String, Object> result = new HashMap<>();
        result.put("status", "success");
        result.put("hdfsPath", hdfsPath);
        return result;
    }

    @Override
    public Map<String, Object> getCustomerBehaviorAnalysis() throws Exception {
        // 执行MapReduce作业
        mapReduceUtil.runCustomerBehaviorAnalysis();
        // 获取结果数据
        Map<String, Object> result = hdfsUtil.readAnalysisResult("/user/hadoop/ecommerce/output/customer-behavior");
        return result;
    }

    @Override
    public Map<String, Object> getProductRecommendationAnalysis() throws Exception {
        mapReduceUtil.runProductRecommendationAnalysis();
        Map<String, Object> result = hdfsUtil.readAnalysisResult("/user/hadoop/ecommerce/output/product-recommendation");
        return result;
    }

    @Override
    public Map<String, Object> getSeasonalTrendAnalysis() throws Exception {
        mapReduceUtil.runSeasonalTrendAnalysis();
        Map<String, Object> result = hdfsUtil.readAnalysisResult("/user/hadoop/ecommerce/output/seasonal-trend");
        return result;
    }

    @Override
    public Map<String, Object> getRegionalMarketAnalysis() throws Exception {
        mapReduceUtil.runRegionalMarketAnalysis();
        Map<String, Object> result = hdfsUtil.readAnalysisResult("/user/hadoop/ecommerce/output/regional-market");
        return result;
    }

    @Override
    public Map<String, Object> getUserRetentionAnalysis() throws Exception {
        mapReduceUtil.runUserRetentionAnalysis();
        Map<String, Object> result = hdfsUtil.readAnalysisResult("/user/hadoop/ecommerce/output/user-retention");
        return result;
    }
}